package Game;
import java.io.Serializable;

/**
 * This class tracks the players current location in the game. It 
 * can provide the distance traveled, distance remaining
 * 
 * @author Ryan Goodrich
 * @version 1.0
 *
 */
public class GPS implements Serializable {

	int startingLocation, currentLocation, nextLocation;
	int distanceTraveled, distanceToNextLocation, totalDistanceToNextLocation;
	boolean traveling;
	
	/**
	 * Create a location class to track the movements of the player
	 * 
	 * @param Starting location id for the player
	 */
	public GPS(int locationId) {
		startingLocation = locationId;
		currentLocation = locationId;
		traveling = false;
	}
	
	/**
	 * Sets the next location to move to
	 * 
	 * @param locaitonId New current location
	 * @param distanceToTravel The distance to be covered toward the next location
	 * @return The choice is valid
	 */
	public boolean setNextLocation(int locationId, int distanceToTravel) {
		
		// trying to go someplace i don't want you to go
		if (traveling && (locationId != currentLocation && locationId != nextLocation))
			return false;
		
		// going to new location
		else if (locationId != currentLocation && !traveling) {
			totalDistanceToNextLocation = GameDriver.getLocationList().getDistanceTo(currentLocation, locationId);
			distanceToNextLocation = totalDistanceToNextLocation;
			
			// location is not a valid location
			if (totalDistanceToNextLocation == -1)
				return false;
		}
		
		// decided to turn around
		else if (locationId == currentLocation && currentLocation != nextLocation)
			distanceToNextLocation = totalDistanceToNextLocation - distanceToNextLocation;
			
		// decrement distance based on pace
		distanceTraveled += Math.min(distanceToNextLocation, distanceToTravel);
		distanceToNextLocation -= distanceToTravel;
		
		// set next location or current location if arrived
		if (distanceToNextLocation <= 0) {
			currentLocation = locationId;
			traveling = false;
		} else {
			nextLocation = locationId;
			traveling = true;
		}
	
		// success
		return true;
	}
	
	/**
	 * Returns the distance remaining to get to next location
	 * 
	 * @return 0 if at next location, distance otherwise
	 */
	public int getDistanceToNextLocation() {
		return Math.max(0, distanceToNextLocation);
	}
	
	/**
	 * Get the current location
	 * 
	 * @return Current location of player, -1 if player is in route to
	 * 		to new location
	 */
	public int getCurrentLocation() {
		return traveling ? -1 : currentLocation;
	}
	
	/**
	 * Get distance from starting location
	 * 
	 * @return Distance from starting location
	 */
	public int getDistanceTraveled() {
		return distanceTraveled;
	}
	
}
